package com.mf.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.mf.config.GlobalParameter;
import com.mf.dto.PositionsDto;
import com.mf.entity.Positions;
import com.mf.entity.code.*;
import com.mf.mapper.PositionsMapper;
import com.mf.service.IPositionsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mf.service.IWorkdayService;
import com.mf.service.code.*;
import com.mf.vo.positions.PositionsByCode;
import com.mf.vo.positions.PositionsByUserListVo;
import com.mf.vo.positions.PositionsVo;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 持仓表 服务实现类
 * </p>
 *
 * @author mf
 * @since 2024-05-18
 */
@Service
public class PositionsServiceImpl extends ServiceImpl<PositionsMapper, Positions> implements IPositionsService {
    @Resource
    private PositionsMapper positionsMapper;
    @Resource
    private IWorkdayService workdayService;
    @Resource
    private ICode600AService code600AService;

    @Resource
    private ICode600BService code600BService;

    @Resource
    private ICode000Service code000Service;

    @Resource
    private ICode001Service code001Service;

    @Resource
    private ICode002AService code002AService;

    @Resource
    private ICode002BService code002BService;

    @Resource
    private ICode003Service code003Service;
    @Resource
    private ICode300AService code300AService;
    @Resource
    private ICode300BService code300BService;
    @Resource
    private ICode301Service code301Service;
    @Resource
    private ICode601Service code601Service;
    @Resource
    private ICode603Service code603Service;
    @Resource
    private ICode605Service code605Service;


    /**
     * 根据持仓列表查询持仓每天的明细
     */
    @Override
    public List<Map<String, Object>>   findPositionsByList(PositionsDto positionsDto) {
        List<Map<String, Object>> result =  new ArrayList<>();
        System.out.println(positionsDto.getUserId());
        final List<PositionsByUserListVo> positionsList = positionsMapper.selectByUserList(positionsDto.getUserId());

        final String date = LocalDate.now().format(GlobalParameter.FORMATTER2);
        for (PositionsByUserListVo positions : positionsList) {
            Map<String, Object> result2 = new HashMap<>();
            final Map<String, Object> selectCode = selectCode(positions.getDm(), positions.getDate(), date);
            result2.put("dm", positions.getDm());
            result2.put("mc", positions.getMc());
            result2.put("date", positions.getDate());
            result2.put("price", positions.getPrice());
            result2.put("quantity", positions.getQuantity());
            result2.put("amount", positions.getAmount());
            result2.put("userName", positions.getUName());
            result2.put("userId", positions.getUserId());
            result2.put("holdingDays", workdayService.findWorkDayByScopeSum(positions.getDate(), date));
            result2.put("positionsList", selectCode);
            result.add(result2);
        }
        return result;
    }

    /**
     * 根据用户id查询持仓
     */
    @Override
    public PositionsVo findPositionsByUserId(Integer userId) {
        List<PositionsByCode> positionsByCode = positionsMapper.findPositionsByUserId(userId);
        // 买入总金额
        BigDecimal amount = new BigDecimal("0");
        for (PositionsByCode byCode : positionsByCode) {
            amount =  amount.add(byCode.getAmount());
        }
        return new PositionsVo(amount, positionsByCode);
    }

    @Override
    public  Map<String, Object> selectCode(String dm,String start,String end) {
        Map<String, Object> result = new HashMap<>();
        if (dm.startsWith("600")) {
            if (Integer.parseInt(dm) < 600500) {
                LambdaQueryWrapper<Code600A> queryWrapper1 = new LambdaQueryWrapper<>();
                queryWrapper1.eq(Code600A::getDm, dm);
                queryWrapper1.between(Code600A::getD, start, end);
                queryWrapper1.orderByAsc(Code600A::getD);

                result.put("data", code600AService.list(queryWrapper1));
            } else {
                LambdaQueryWrapper<Code600B> queryWrapper1 = new LambdaQueryWrapper<>();
                queryWrapper1.eq(Code600B::getDm, dm);
                queryWrapper1.between(Code600B::getD, start, end);
                queryWrapper1.orderByAsc(Code600B::getD);
                result.put("data", code600BService.list(queryWrapper1));
            }
        }else if(dm.startsWith("000")){
            LambdaQueryWrapper<Code000> queryWrapper1 = new LambdaQueryWrapper<>();
            queryWrapper1.eq(Code000::getDm, dm);
            queryWrapper1.between(Code000::getD, start, end);
            queryWrapper1.orderByAsc(Code000::getD);
            result.put("data", code000Service.list(queryWrapper1));
        }else if(dm.startsWith("001")){
            LambdaQueryWrapper<Code001> queryWrapper1 = new LambdaQueryWrapper<>();
            queryWrapper1.eq(Code001::getDm, dm);
            queryWrapper1.between(Code001::getD, start, end);
            queryWrapper1.orderByAsc(Code001::getD);
            result.put("data", code001Service.list(queryWrapper1));
        }else if(dm.startsWith("002")){
            if (Integer.parseInt(dm) < 2500) {
                LambdaQueryWrapper<Code002A> queryWrapper1 = new LambdaQueryWrapper<>();
                queryWrapper1.eq(Code002A::getDm, dm);
                queryWrapper1.orderByAsc(Code002A::getD);
                queryWrapper1.between(Code002A::getD, start, end);

                result.put("data", code002AService.list(queryWrapper1));
            } else  {
                LambdaQueryWrapper<Code002B> queryWrapper1 = new LambdaQueryWrapper<>();
                queryWrapper1.eq(Code002B::getDm, dm);
                queryWrapper1.between(Code002B::getD, start, end);
                queryWrapper1.orderByAsc(Code002B::getD);
                final List<Code002B> list = code002BService.list(queryWrapper1);
                System.out.println(list);
                result.put("data",list );
            }
        }else if(dm.startsWith("003")){
            LambdaQueryWrapper<Code003> queryWrapper1 = new LambdaQueryWrapper<>();
            queryWrapper1.eq(Code003::getDm, dm);
            queryWrapper1.between(Code003::getD, start, end);
            queryWrapper1.orderByAsc(Code003::getD);
            result.put("data", code003Service.list(queryWrapper1));
        }else if(dm.startsWith("300")){
            if (Integer.parseInt(dm) < 300500) {
                LambdaQueryWrapper<Code300A> queryWrapper1 = new LambdaQueryWrapper<>();
                queryWrapper1.eq(Code300A::getDm, dm);
                queryWrapper1.between(Code300A::getD, start, end);
                queryWrapper1.orderByAsc(Code300A::getD);
                result.put("data", code300AService.list(queryWrapper1));
            } else  {
                LambdaQueryWrapper<Code300B> queryWrapper1 = new LambdaQueryWrapper<>();
                queryWrapper1.eq(Code300B::getDm, dm);
                queryWrapper1.between(Code300B::getD, start, end);
                queryWrapper1.orderByAsc(Code300B::getD);
                result.put("data", code300BService.list(queryWrapper1));
            }
        }else if(dm.startsWith("301")){
            LambdaQueryWrapper<Code301> queryWrapper1 = new LambdaQueryWrapper<>();
            queryWrapper1.eq(Code301::getDm, dm);
            queryWrapper1.between(Code301::getD, start, end);
            queryWrapper1.orderByAsc(Code301::getD);
            result.put("data", code301Service.list(queryWrapper1));
        }else if(dm.startsWith("601")){
            LambdaQueryWrapper<Code601> queryWrapper1 = new LambdaQueryWrapper<>();
            queryWrapper1.eq(Code601::getDm, dm);
            queryWrapper1.between(Code601::getD, start, end);
            queryWrapper1.orderByAsc(Code601::getD);
            result.put("data", code601Service.list(queryWrapper1));
        }else if(dm.startsWith("603")){
            LambdaQueryWrapper<Code603> queryWrapper1 = new LambdaQueryWrapper<>();
            queryWrapper1.eq(Code603::getDm, dm);
            queryWrapper1.between(Code603::getD, start, end);
            queryWrapper1.orderByAsc(Code603::getD);
            result.put("data", code603Service.list(queryWrapper1));
        }else if(dm.startsWith("605")){
            LambdaQueryWrapper<Code605> queryWrapper1 = new LambdaQueryWrapper<>();
            queryWrapper1.eq(Code605::getDm, dm);
            queryWrapper1.between(Code605::getD, start, end);
            queryWrapper1.orderByAsc(Code605::getD);
            result.put("data", code605Service.list(queryWrapper1));
        }
        return result;
    }

}
